package com.bancomer.pig.functions;

public class AmountValidator {
	
	public static int validateAmount(String officeType, double amount, String condition){
		int result = 1;
		String [] conditions = condition.split(";");
		String defaultOperator="==";
		String defaultAmount="0.0";
		boolean indicator = false;
		for(int i=0;i<conditions.length;i++){
			String officeTypeCondition = conditions[i].split(",")[0];
			officeTypeCondition = (officeTypeCondition==null)?"":officeTypeCondition;
			officeType = (officeType==null)?"":officeType;
			if("".equals(officeTypeCondition)){
				defaultOperator = conditions[i].split(",")[1];
				defaultAmount = conditions[i].split(",")[2];
			}
			if("".equals(officeType.trim())){
				break;
			}
			else if(officeTypeCondition.equals(officeType.trim())){
				String operatorCondition = (conditions[i].split(",")[1]).trim();
				String amountCondition = (conditions[i].split(",")[2]).trim();
				indicator = true;
				if(operatorCondition.equals(">")){
					result = (amount>Double.parseDouble(amountCondition))?1:0;
				}
				else if(operatorCondition.equals("<")){
					result = (amount<Double.parseDouble(amountCondition))?1:0;
				}
				else if(operatorCondition.equals(">=")){
					result = (amount>=Double.parseDouble(amountCondition))?1:0;
				}
				else if(operatorCondition.equals("<=")){
					result = (amount<=Double.parseDouble(amountCondition))?1:0;
				}
				else if(operatorCondition.equals("==")){
					result = (amount==Double.parseDouble(amountCondition))?1:0;
				}
				break;
			}
		}
		if(indicator==false){
			if(defaultOperator.equals(">")){
				result = (amount>Double.parseDouble(defaultAmount))?1:0;
			}
			else if(defaultOperator.equals("<")){
				result = (amount<Double.parseDouble(defaultAmount))?1:0;
			}
			else if(defaultOperator.equals(">=")){
				result = (amount>=Double.parseDouble(defaultAmount))?1:0;
			}
			else if(defaultOperator.equals("<=")){
				result = (amount<=Double.parseDouble(defaultAmount))?1:0;
			}
			else if(defaultOperator.equals("==")){
				result = (amount==Double.parseDouble(defaultAmount))?1:0;
			}
		}
		return result;
	}
}
